package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.util.AppJwtUtil;
import com.heima.user.dto.LoginDto;
import com.heima.user.entity.ApUser;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.IApUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * APP用户信息表 服务实现类
 * </p>
 *
 * @author lj
 * @since 2022-06-27
 */
@Service
public class ApUserServiceImpl extends ServiceImpl<ApUserMapper, ApUser> implements IApUserService {

    /**
     * App用户登录
     * @param dto
     * @return
     */
    @Override
    public ResponseResult login(LoginDto dto) {
        // 接收用户对象判断，设备id判断
        if (dto==null|| StringUtils.isBlank(dto.getEquipmentId())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        // 用户名和密码，仅有设备id用于浏览
        if (StringUtils.isNotBlank(dto.getPhone())&&StringUtils.isNotBlank(dto.getPassword())) {
            LambdaQueryWrapper<ApUser> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ApUser::getPhone,dto.getPhone());
            ApUser one = this.getOne(queryWrapper);
            if (one==null) {
                return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST);
            }
            String password = DigestUtils.md5Hex(dto.getPassword() + one.getSalt());
            if (!StringUtils.equals(password,one.getPassword())) {
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
            }
            // 构造结果集
            Map resultMap = new HashMap();
            // 用户信息，去除敏感信息
            one.setPhone("");
            one.setPassword("");
            one.setSalt("");
            resultMap.put("user",one);
            // token，根据JWT生成
            Map<String,Object> claimMaps = new HashMap<>();
            claimMaps.put("userId",one.getId());
            claimMaps.put("name",one.getName());
            String token = AppJwtUtil.getToken(claimMaps);
            resultMap.put("token",token);
            // 返回结果
            return ResponseResult.okResult(resultMap);
        }else {
            // 未登录，仅是进行数据浏览
            // 结果集构造
            Map resultMap = new HashMap();
            // 设置用户id=0
            ApUser one = new ApUser();
            one.setId(0);
            resultMap.put("user",one);
            // 设置token
            Map<String,Object> claimMaps = new HashMap<>();
            claimMaps.put("userId",one.getId());
            claimMaps.put("name","");
            claimMaps.put("equipmentId",dto.getEquipmentId());
            String token = AppJwtUtil.getToken(claimMaps);
            resultMap.put("token",token);
            // 返回结果
            return ResponseResult.okResult(resultMap);
        }
    }
}
